using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace CuthluralSwitcher.Library
{
    public class WallpaperManager
    {
        private enum WallpaperPos
        {
            Centre = 0,
            Tiled,
            Streched
        };

        private enum Sort
        {
            Ascending = 0,
            Descending = 1
        };

        private static string WALLPAPER_TEMP_FILE = Path.Combine(Path.GetTempPath(), "tempwall.bmp");

        private const int SPI_SETDESKWALLPAPER = 20;
        private const int SPIF_UPDATEINIFILE = 0x01;
        private const int SPIF_SENDWININICHANGE = 0x02;
        private const int COLOR_BACKGROUND = 1;
        private const int SPI_GETSCREENSAVEACTIVE = 16;
        private const int SPI_SETSCREENSAVEACTIVE = 17;
        private const int GW_CHILD = 5;
        private const int SW_HIDE = 0;
        private const int SW_SHOW = 5;
        private const int MOD_ALT = 0x0001;
        private const int MOD_CONTROL = 0x0002;
        private const int MOD_SHIFT = 0x0004;
        private const int MOD_WIN = 0x0008;
        private const int WM_HOTKEY = 0x0312;

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int SystemParametersInfo(
            int uAction, int uParam, string lpvParam, int fuWinIni);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int SystemParametersInfo(
            int uAction, int uParam, ref int lpvParam, int fuWinIni);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int SystemParametersInfo(
            int uAction, int uParam, int lpvParam, int fuWinIni);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern bool SetSysColors(int cElements, int[] lpaElements, int[] lpaRgbValues);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int FindWindowEx(int hWndParent, int hwndChildAfter, string lpszClassName, string lpszWindow);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int GetWindow(int hWnd, int fuRel);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int ShowWindow(int hWnd, int nCmdShow);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern uint GetCurrentDirectory(uint nBufferLength, [Out] StringBuilder lpBuffer);


        public static void SetWallpaper(Image image)
        {
            if (File.Exists(WALLPAPER_TEMP_FILE))
            {
                File.Delete(WALLPAPER_TEMP_FILE);
            }
            image.Save(WALLPAPER_TEMP_FILE, ImageFormat.Bmp);

            int[] nElements = { COLOR_BACKGROUND };
            int[] nColors = { System.Drawing.ColorTranslator.ToWin32(Color.Black) };
            SetSysColors(nElements.Length, nElements, nColors);

            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, WALLPAPER_TEMP_FILE, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
        }
    }

}
